/**
 * API 相关类型定义
 */

import type { ResponseStatus } from '~/utils/api-client'

// ===== 基础类型 =====

export interface ApiResponse<T = any> {
  success: boolean
  data?: T
  message?: string
  code?: number
  status: ResponseStatus
}

export interface RequestConfig {
  headers?: Record<string, string>
  timeout?: number
  retryTimes?: number
  withToken?: boolean
  signal?: AbortSignal
}

// ===== 认证相关类型 =====

export interface LoginCredentials {
  username: string
  password: string
}

export interface AuthTokens {
  token: string
  refreshToken?: string
  expiresIn?: number
}

export interface LoginResponse extends ApiResponse<AuthTokens> {}

// ===== 影响者相关类型 =====

export interface InfluencerScore {
  channelId: string
  channelName: string
  score: number
  maxScore: number
  riskLevel: 'low' | 'medium' | 'high' | 'critical'
  lastUpdated: string
  factors: {
    reportCount: number
    contentQuality: number
    userFeedback: number
    platformViolations: number
  }
}

export interface InfluencerScoreResponse extends ApiResponse<InfluencerScore> {}

// ===== 举报相关类型 =====

export interface ReportData {
  channelId: string
  type: ReportType
  description: string
  anonymous?: boolean
  evidence?: string[]
  tags?: string[]
}

export interface Report {
  id: string
  channelId: string
  channelName: string
  type: ReportType
  description: string
  status: ReportStatus
  anonymous: boolean
  reportedAt: string
  updatedAt: string
  reportedBy?: string
  evidence?: string[]
  tags?: string[]
  adminNotes?: string
}

export interface ReportHistory {
  channelId: string
  channelName: string
  totalReports: number
  reports: Report[]
  summary: {
    byType: Record<ReportType, number>
    byStatus: Record<ReportStatus, number>
    recentTrend: 'increasing' | 'decreasing' | 'stable'
  }
}

export interface CreateReportResponse extends ApiResponse<{ reportId: string }> {}
export interface ReportHistoryResponse extends ApiResponse<ReportHistory> {}

// ===== 枚举类型 =====

export enum ReportType {
  SPAM = 'spam',
  FAKE_NEWS = 'fake_news',
  HATE_SPEECH = 'hate_speech',
  MISLEADING_CONTENT = 'misleading_content',
  COPYRIGHT_VIOLATION = 'copyright_violation',
  HARASSMENT = 'harassment',
  SCAM = 'scam',
  INAPPROPRIATE_CONTENT = 'inappropriate_content',
  OTHER = 'other',
}

export enum ReportStatus {
  PENDING = 'pending',
  REVIEWING = 'reviewing',
  VERIFIED = 'verified',
  REJECTED = 'rejected',
  RESOLVED = 'resolved',
}

// ===== 分页和筛选 =====

export interface PaginationParams {
  page?: number
  limit?: number
  sortBy?: string
  sortOrder?: 'asc' | 'desc'
}

export interface FilterParams {
  reportType?: ReportType[]
  reportStatus?: ReportStatus[]
  dateFrom?: string
  dateTo?: string
  riskLevel?: InfluencerScore['riskLevel'][]
}

export interface PaginatedResponse<T> {
  items: T[]
  pagination: {
    page: number
    limit: number
    total: number
    pages: number
    hasNext: boolean
    hasPrev: boolean
  }
}

// ===== 统计数据类型 =====

export interface ChannelStatistics {
  channelId: string
  channelName: string
  subscriberCount: number
  videoCount: number
  viewCount: number
  joinedDate: string
  lastActive: string
  verificationStatus: 'verified' | 'unverified'
  categories: string[]
}

export interface ReportStatistics {
  totalReports: number
  reportsByType: Record<ReportType, number>
  reportsByStatus: Record<ReportStatus, number>
  reportsByMonth: Array<{
    month: string
    count: number
  }>
  topReportedChannels: Array<{
    channelId: string
    channelName: string
    reportCount: number
    riskLevel: InfluencerScore['riskLevel']
  }>
}

// ===== 用户相关类型 =====

export interface User {
  id: string
  username: string
  email: string
  role: 'user' | 'moderator' | 'admin'
  joinedDate: string
  isActive: boolean
  permissions: string[]
}

export interface UserProfile extends User {
  preferences: {
    notifications: boolean
    anonymousReporting: boolean
    language: string
    theme: 'light' | 'dark' | 'auto'
  }
  statistics: {
    reportsSubmitted: number
    reportsVerified: number
    contributionScore: number
  }
}

// ===== 错误类型 =====

export interface ApiError {
  code: string
  message: string
  details?: any
  timestamp: string
  path?: string
}

export interface ValidationError extends ApiError {
  field: string
  value: any
  constraint: string
}

// ===== 实用工具类型 =====

export type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
}

export type RequiredFields<T, K extends keyof T> = T & Required<Pick<T, K>>

export type OptionalFields<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

// ===== API 端点类型映射 =====

export interface ApiEndpoints {
  // 认证
  login: {
    request: LoginCredentials
    response: LoginResponse
  }
  refreshToken: {
    request: { refreshToken: string }
    response: LoginResponse
  }
  logout: {
    request: Record<string, never>
    response: ApiResponse
  }

  // 影响者数据
  getInfluencerScore: {
    params: { channelId: string }
    response: InfluencerScoreResponse
  }

  // 举报
  createReport: {
    request: ReportData
    response: CreateReportResponse
  }
  getReportHistory: {
    params: { channelId: string }
    query?: PaginationParams & FilterParams
    response: ReportHistoryResponse
  }
  getUserReports: {
    query?: PaginationParams & FilterParams
    response: ApiResponse<PaginatedResponse<Report>>
  }

  // 统计
  getReportStatistics: {
    query?: FilterParams
    response: ApiResponse<ReportStatistics>
  }
}
